package cn.aran.pet.service.impl;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.utils.*;
import cn.aran.order.domain.OrderPetAcquisition;
import cn.aran.order.mapper.OrderPetAcquisitionMapper;
import cn.aran.org.domain.Shop;
import cn.aran.org.mapper.ShopMapper;
import cn.aran.pet.domain.Pet;
import cn.aran.pet.domain.SearchMasterMsg;
import cn.aran.pet.domain.SearchMasterMsgAuditLog;
import cn.aran.pet.mapper.PetMapper;
import cn.aran.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.aran.pet.mapper.SearchMasterMsgMapper;
import cn.aran.pet.service.ISearchMasterMsgService;
import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.pet.vo.HandlerMsgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-19
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg) {
        // 保存
        searchMasterMsgMapper.save(searchMasterMsg);
        // 智能校验
        StringBuffer sb = new StringBuffer()
                .append(searchMasterMsg.getName())
                .append(searchMasterMsg.getTitle())
                .append(searchMasterMsg.getAddress())
                .append(searchMasterMsg.getCoatColor())
                .append(searchMasterMsg.getNote());
        if (!BaiduAuditUtils.TextCensor(sb.toString())){
            searchMasterMsg.setState(-1);
            SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
            searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
            searchMasterMsgAuditLog.setState(0);
            searchMasterMsgAuditLog.setAuditTime(new Date());
            searchMasterMsgAuditLog.setNote("上传的信息的不合规,标题,名称,地址,毛色,备注");
            // 发送信息告知用户
            // 保存
            searchMasterMsgAuditLogMapper.save(searchMasterMsgAuditLog);
        }else {
            searchMasterMsg.setState(1);
            // 获取用户地址坐标
            Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
            // 获取附近的店铺信息
            List<Shop> shops = shopMapper.loadAll();
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            if (nearestShop != null){
                searchMasterMsg.setShopId(nearestShop.getId());
                // 发送信息告知店铺
            }

        }
        searchMasterMsgMapper.update(searchMasterMsg);

    }

    /**
    * @Title: accept
    * @Description: 店铺员工接单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 23:04
    * @Parameters: [handlerMsgDto]
    * @Return void
    */
    @Override
    public void accept(HandlerMsgVo handlerMsgDto) {
        // 非空校验
        // 加锁防止多人同时接单
        // 1.查询此寻主消息是否已经被接单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadByOrderReceived(handlerMsgDto.getMsgId());
        // 如果查询到了收购单，那么说明此消息已被接单，提示用户
        if (orderPetAcquisition != null){
            throw new BusinessException("此消息已被接单！");
        }
        // 1.查询寻主消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(handlerMsgDto.getMsgId());
        // 2.生成宠物信息，并保存
        Pet pet = msg2Pet(searchMasterMsg);
        petMapper.save(pet); // 需要返回主键
        // 3.初始化订单对象并保存
        orderPetAcquisition = initOrderPetAcquisition(handlerMsgDto, searchMasterMsg, pet);
        orderPetAcquisitionMapper.save(orderPetAcquisition);

    }

    /**
    * @Title: reject
    * @Description: 店铺拒单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 23:32
    * @Parameters: [msgId]
    * @Return void
    */
    @Override
    public void reject(Long msgId) {
        // 1.参数校验
        // 2.根据消息Id查询寻主消息，将shopId置空，并修改
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(msgId);
        if (searchMasterMsg == null){
            throw new BusinessException("此订单不存在！");
        }
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 3.记录日志，自行完成
    }

    private OrderPetAcquisition initOrderPetAcquisition(HandlerMsgVo handlerMsgDto, SearchMasterMsg searchMasterMsg, Pet pet) {
        OrderPetAcquisition orderPetAcquisition = new OrderPetAcquisition();
        // 使用工具类生成订单编号
        String orderSn = CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUserId());
        orderPetAcquisition.setOrderSn(orderSn);
        orderPetAcquisition.setDigest(handlerMsgDto.getNote());
        // 最迟的处理订单的时间
        orderPetAcquisition.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        // 这时候还不是最终价格，因为我们销售人员可以去讲价，加价之后的才是最终价格
        orderPetAcquisition.setPrice(searchMasterMsg.getPrice());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisition.setUserId(searchMasterMsg.getUserId());
        orderPetAcquisition.setShopId(searchMasterMsg.getShopId());
        orderPetAcquisition.setAddress(searchMasterMsg.getAddress());
        orderPetAcquisition.setEmpId(handlerMsgDto.getHandler());
        orderPetAcquisition.setSearchMasterMsgId(searchMasterMsg.getId());
        return orderPetAcquisition;
    }


    private Pet msg2Pet(SearchMasterMsg searchMasterMsg) {
        // 2.根据寻主消息初始化宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(searchMasterMsg.getPrice());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        return pet;
    }
}
